//
//  tooles.m
//  huoche
//
//  Created by kan xu on 11-1-22.
//  Copyright 2011 paduu. All rights reserved.
//

#import "tooles.h"
#import "DataPlist.h"

@implementation tooles

//自定义的对话框
+(void)showSelectMsgBoxWithTitle:(NSString*)title andMsg:(NSString*)msg andDelegate:(id<UIAlertViewDelegate>)target
{
    NSString *cachetitle = [title length] > 0? title:@"提示";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:cachetitle
                                                        message:msg
                                                       delegate:target
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定",nil];
    [alertView setAlertViewStyle:UIAlertViewStyleDefault];
    [alertView show];
}

+(void)showInputMsgBoxWithTitle:(NSString*)title andMsg:(NSString*)msg andDelegate:(id<UIAlertViewDelegate>)target
{
    NSString *cachetitle = [title length] > 0? title:@"提示";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:cachetitle
                                                        message:msg
                                                       delegate:target
                                              cancelButtonTitle:@"取消"
                                              otherButtonTitles:@"确定",nil];
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [alertView show];
}

+(void)showAlertMsgBoxWithTitle:(NSString*)title andMsg:(NSString*)msg
{
    NSString *cachetitle = [title length] > 0? title:@"提示";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:cachetitle
                                                     message:msg
                                                    delegate:nil
                                           cancelButtonTitle:nil
                                              otherButtonTitles:@"确认", nil];
    [alertView setAlertViewStyle:UIAlertViewStyleDefault];
    [alertView show];
}

+(void)showAlertMsgDetailedWithTitle:(NSString*)title andMsg:(NSString*)msg
{
    NSString *cachetitle = [title length] > 0? title:@"详细内容";
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:cachetitle
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:@"确认", nil];
    [alertView setAlertViewStyle:UIAlertViewStyleDefault];
    [alertView show];
}

+(UIImage*)getImageByColor:(UIColor*)aColor
{
    CGSize as = CGSizeMake(1, 1);
    UIImage *img = nil;
    CGRect rect = CGRectMake(0, 0, as.width, as.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,
                                   aColor.CGColor);
    CGContextFillRect(context, rect);
    img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}

+ (UIViewController *)defaultViewControllerWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (KDeviceHeight==568) {
        //iphone5,later
        return [[NSClassFromString(nibNameOrNil) alloc]initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    }
    else
        return nil;
}

+ (NSString *)defaultViewControllerWithNibName:(NSString *)nibNameOrNil
{
    if (KDeviceHeight==480) {
        //iphone 3G,4,4s
        return [NSString stringWithFormat:@"%@_4",nibNameOrNil];
    }
    return nibNameOrNil;
}

+(float)DeviceVersion
{
    return [[UIDevice currentDevice].systemVersion doubleValue];
}

+(UIImage *)grayImage:(UIImage *)sourceImage
{
    int bitmapInfo = kCGImageAlphaNone;
    int width = sourceImage.size.width;
    int height = sourceImage.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    CGContextRef context = CGBitmapContextCreate (nil,
                                                  width,
                                                  height,
                                                  8,      // bits per component
                                                  0,
                                                  colorSpace,
                                                  bitmapInfo);
    CGColorSpaceRelease(colorSpace);
    if (context == NULL) {
        return nil;
    }
    CGContextDrawImage(context,
                       CGRectMake(0, 0, width, height), sourceImage.CGImage);
    UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
    CGContextRelease(context);
    return grayImage;
}

+(NSString *)wubiSentenceByChara:(NSString*)aCha
{
    unichar cacheChar = [aCha characterAtIndex:0];
    switch (cacheChar - 'a') {
        case 0:
            return @"工戈草头右框七";
        case 1:
            return @"子耳了也框向上";
        case 2:
            return @"又巴马 丢矢矣";
        case 3:
            return @"大犬三羊古石厂";
        case 4:
            return @"月彡乃用家衣底";
        case 5:
            return @"土十二干士寸雨";
        case 6:
            return @"王旁青头兼五一";
        case 7:
            return @"目具上止卜虎皮";
        case 8:
            return @"水旁兴头小倒立";
        case 9:
            return @"日早两竖与虫依";
        case 10:
            return @"口与川 字根稀";
        case 11:
            return @"田甲方框四车力";
        case 12:
            return @"山由贝 下框几";
        case 13:
            return @"已半巳满不出己 左框折尸心和羽";
        case 14:
            return @"火业头 四点米";
        case 15:
            return @"之宝盖 摘示衣";
        case 16:
            return @"金勺缺点无尾鱼 犬旁留乂儿一点夕 氏无七";
        case 17:
            return @"白手看头三二斤";
        case 18:
            return @"木丁西";
        case 19:
            return @"禾竹一撇双人立 反文条头共三一";
        case 20:
            return @"立辛两点六门病";
        case 21:
            return @"女刀九臼山朝西";
        case 22:
            return @"人和八 三四里";
        case 23:
            return @"慈母无心弓和匕 幼无力";
        case 24:
            return @"言文方广在四一 高头一捺谁人去";
        case 25:
            return @"";
        default:
            return @"";
    }
}

+(int)getPlistValueForKey:(NSString*)aStr
{
    NSMutableDictionary *levelDic = [DataPlist openPlistFromDocumentWithName:@"level" AndTheType:@"plist"];
    NSLog(@"fffff:%@",levelDic);
    if(!levelDic)
    {
        NSLog(@"not exist");
        levelDic = [NSMutableDictionary dictionary];
        [levelDic setValue:@"1" forKey:@"level"];
        [levelDic setValue:@"0" forKey:@"bought"];
        [DataPlist writePlistToDocumentWithDic:levelDic WithName:@"level" AndTheType:@"plist"];
        return [[levelDic valueForKey:aStr] intValue];
    }
    return [[levelDic valueForKey:aStr] intValue];
}

+(void)setPlistValue:(int)aValue ForKey:(NSString*)aStr
{
    NSMutableDictionary *levelDic = [DataPlist openPlistFromDocumentWithName:@"level" AndTheType:@"plist"];
    [levelDic setValue:[NSString stringWithFormat:@"%d",aValue] forKey:aStr];
    [DataPlist writePlistToDocumentWithDic:levelDic WithName:@"level" AndTheType:@"plist"];
}

+(int)getIntFromData:(NSData*)aData{
    const char* pos = [aData bytes];
    int a;
    memcpy(&a, pos, sizeof(int));
    return a;
}

@end
